package com.hit.aircraftwar.aircraft;



import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.hit.aircraftwar.activity.MainActivity;
import com.hit.aircraftwar.application.ImageManager;
import com.hit.aircraftwar.bullet.AbstractBullet;
import com.hit.aircraftwar.bullet.HeroBullet;
import com.hit.aircraftwar.bullet.Missile;
import com.hit.aircraftwar.setter.ParameterSet;
import com.hit.aircraftwar.strategy.ShootStrategy;
import com.hit.aircraftwar.strategy.SpreadShootStrategy;
import com.hit.aircraftwar.strategy.StraightShootStrategy;

import java.util.List;

/**
 * 英雄飞机，游戏玩家操控
 * @author hitsz
 */
public class HeroAircraft extends AbstractAircraft {

    private static final HeroAircraft INSTANCE = new HeroAircraft(
            MainActivity.width / 2,
            MainActivity.height / 4 * 3,
            0, 0,
            3000,
                new StraightShootStrategy(),
            -1,
            35,
            2
        );


    public void restart(){
        INSTANCE.isValid = true;
        INSTANCE.setLocation(MainActivity.width/2, MainActivity.height/4 * 3);
        INSTANCE.setShootStrategy(new StraightShootStrategy());
        INSTANCE.hp = maxHp;
        INSTANCE.direction = -1;
        switch(ParameterSet.INSTANCE.getHeroType()) {
            case 1:
                INSTANCE.shootNum = 2;
                maxShootNum = 7;
                break;
            case 2:
                INSTANCE.shootNum = 1;
                maxShootNum = 3;
                break;
        }
    }

    public static HeroAircraft getInstance() {
        return INSTANCE;
    }

    protected int maxShootNum = 7;

    /**
     * @param locationX 英雄机位置x坐标
     * @param locationY 英雄机位置y坐标
     * @param speedX 英雄机射出的子弹的基准速度（英雄机无特定速度）
     * @param speedY 英雄机射出的子弹的基准速度（英雄机无特定速度）
     * @param hp    初始生命值
     */
    private HeroAircraft(int locationX, int locationY, int speedX, int speedY, int hp, ShootStrategy shootStrategy, int direction, int power, int shootNum) {
        super(locationX, locationY, speedX, speedY, hp, shootStrategy, direction, power, shootNum);
    }

    @Override
    public void forward() {
        // 英雄机由鼠标控制，不通过forward函数移动
    }


    @Override
    public List<AbstractBullet> shoot(AbstractAircraft abstractAircraft) {
        return abstractAircraft.shootStrategy.shoot(abstractAircraft);
    }

    public void addShootNum(int add) {
        if(this.shootNum + add <= maxShootNum){
            this.shootNum += add;
        } else {
            this.shootNum = maxShootNum;
        }
        if(shootNum > 1) {
            this.setShootStrategy(new SpreadShootStrategy());
        }
    }

    public void restoreShootNum() {
        switch(ParameterSet.INSTANCE.getHeroType()) {
            case 1:
                INSTANCE.shootNum = 2;
                maxShootNum = 7;
                break;
            case 2:
                INSTANCE.shootNum = 1;
                maxShootNum = 3;
                break;
        }
        INSTANCE.setShootStrategy(new StraightShootStrategy());
    }

    @Override
    public void bombed() {
    }

    @Override
    public void draw(Canvas canvas, Paint paint){
        Bitmap bitmap;
        switch(ParameterSet.INSTANCE.getHeroType()) {
            case 1:
                bitmap = ImageManager.HERO_BITMAP;
                break;
            case 2:
                bitmap = ImageManager.HERO2_BITMAP;
                break;
            default:
                bitmap = getBitmap();
        }
        if(this.isValid) {
            canvas.drawBitmap(bitmap, (int)(this.getLocationX() - bitmap.getWidth() / 2.0), (int)(this.getLocationY() - bitmap.getHeight() / 2.0), paint);
        }
    }
}
